
package vng.kimcuong;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;

class GL2JNIView extends GLSurfaceView {
    static Context context;

    public GL2JNIView(Context context, int width, int height) {
        super(context);
        GL2JNIView.context = context;
        setRenderer(new Renderer(width, height));
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	switch (event.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN:
				NativeLib.touchdown((short)event.getX(), (short)event.getY());
				break;
			case MotionEvent.ACTION_UP:
				NativeLib.touchup((short)event.getX(), (short)event.getY());
				break;
			case MotionEvent.ACTION_MOVE:
				NativeLib.touchmove((short)event.getX(), (short)event.getY());
				break;
    	}
    	return true;
    }

    private static class Renderer implements GLSurfaceView.Renderer {
    	int width, height;
    	public Renderer(int width, int height)
    	{
    		this.width = width;
    		this.height = height;
    	}
        public void onDrawFrame(GL10 gl) {
        	NativeLib.update();
        	NativeLib.render();
        }

        public void onSurfaceChanged(GL10 gl, int width, int height) {
        	
        }

        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        	String apkFilePath = null;
    		ApplicationInfo appInfo = null;
    		PackageManager packMgmr = context.getPackageManager();
    		try {
    			appInfo = packMgmr.getApplicationInfo("vng.kimcuong", 0);
	        } catch (NameNotFoundException e) {
	    	    e.printStackTrace();
	    	    throw new RuntimeException("Unable to locate assets, aborting...");
	        }
	    		apkFilePath = appInfo.sourceDir;
	        	NativeLib.init(width, height, apkFilePath);
	        }
    }
}
